<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库">
              <j-search-select-tag v-model="storeId" dict="biz_store,store_name,id" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓位">
              <a-select v-model="spaceId" allowClear placeholder="请选择">
                <a-select-option :value="item.id" v-for="(item, index) in spaceList" :key="index">
                  {{ item.spaceCode }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <template v-if="toggleSearchStatus"> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品">
              <j-search-select-tag v-model="goodsId" dict="biz_goods,name,id" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="规格">
              <a-select v-model="specsId" allowClear placeholder="请选择">
                <a-select-option :value="item.id" v-for="(item, index) in goodsSkuList" :key="index">
                  {{ item.specsValues }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- </template> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery1" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 begin -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('库存报表(仓位)')">导出</a-button>
    </div>
    <!-- 操作按钮区域 end -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>

    <space-goods-detail-modal ref="modalForm" @ok="modalFormOk"></space-goods-detail-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import SpaceGoodsDetailModal from './modules/SpaceGoodsDetailModal'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import { getStockPosition, getQuerySpecsNameByGoodsId } from '@/api/storeApproval'

export default {
  name: 'SpaceGoodsDetailList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JSearchSelectTag,
    SpaceGoodsDetailModal,
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  watch: {
    storeId: {
      handler: function (val) {
        this.spaceList = []
        this.spaceId = ''
        getStockPosition({ id: val }).then((res) => {
          if (res.success) {
            this.spaceList = res.result.records
          }
        })
      },
    },
    goodsId: {
      handler: function (val) {
        this.goodsSkuList = []
        this.specsId = ''
        getQuerySpecsNameByGoodsId({ goodsId: val }).then((res) => {
          if (res.success) {
            this.goodsSkuList = res.result
          }
        })
      },
    },
  },
  data() {
    return {
      description: '库位商品明细表管理页面',
      storeId: '',
      goodsId: '',
      spaceId: '',
      specsId: '',
      spaceList: null,
      goodsSkuList: null,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '仓库',
          align: 'center',
          dataIndex: 'storeId_dictText',
          sorter: true,
        },
        {
          title: '仓位',
          align: 'center',
          dataIndex: 'spaceId_dictText',
          sorter: true,
        },
        {
          title: '商品',
          align: 'center',
          dataIndex: 'goodsId_dictText',
          sorter: true,
        },
        {
          title: '规格',
          align: 'center',
          dataIndex: 'specsId_dictText',
          sorter: true,
        },
        {
          title: '商品数量',
          align: 'center',
          dataIndex: 'num',
        },
        {
          title: '库存单位',
          align: 'center',
          dataIndex: 'unitId_dictText',
          customRender: (text, record, index) => {
            if (!text) {
              return <span style="color:red">未设置</span>
            } else {
              return text
            }
          },
        },
        {
          title: '加权平均价',
          align: 'center',
          dataIndex: 'price',
        },
      ],
      url: {
        list: '/storage/spaceGoodsDetail/list',
        delete: '/storage/spaceGoodsDetail/delete',
        deleteBatch: '/storage/spaceGoodsDetail/deleteBatch',
        exportXlsUrl: '/storage/spaceGoodsDetail/exportXls',
        importExcelUrl: 'storage/spaceGoodsDetail/importExcel',
      },
      dictOptions: {},
    }
  },
  created() {},

  methods: {
    initDictConfig() {},
    searchReset1() {
      this.specsId = this.goodsId = this.spaceId = this.storeId = ''
      this.searchReset()
    },
    searchQuery1() {
      let storeId = this.storeId
      let spaceId = this.spaceId
      let goodsId = this.goodsId
      let specsId = this.specsId
      this.queryParam = {
        storeId,
        spaceId,
        goodsId,
        specsId,
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>